package com.tianqu.tms.www.transfer.controller;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsController;
import com.tianqu.tms.core.base.TxController;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.exception.TmsDaoException;
import com.tianqu.tms.core.exception.TmsException;
import com.tianqu.tms.core.exception.TransactionException;
import com.tianqu.tms.core.security.TmsUserProfile;
import com.tianqu.tms.core.util.StringUtil;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.entity.Car;
import com.tianqu.tms.entity.Codes;
import com.tianqu.tms.entity.TransMarketCode;
import com.tianqu.tms.entity.TransferAcceptedInfo;
import com.tianqu.tms.entity.TransferPersonalCarInfo;
import com.tianqu.tms.entity.TransferProcessHistory;
import com.tianqu.tms.www.transfer.actionform.TransferRgActionForm;
import com.tianqu.tms.www.transfer.viewhelper.TransferRgInputViewHelper;

public class TransferSeniorChangeJYPResultController extends TmsController {
	private Logger logger = Logger.getLogger(TransferSeniorChangeJYPResultController.class);
	@Override
	public void execute(UserContext uc) throws Exception {
		logger.debug(this.getClass().getName() + "#execute was called.");
		TxController controller = getTxController();
		try {
			controller.begin();
			TmsUserProfile profile = uc.getUserProfile();

			TransferRgActionForm form = (TransferRgActionForm)uc.getInputForm();
			TransferRgInputViewHelper view =(TransferRgInputViewHelper)uc.getAttribute(TmsConstants.VIEWHELP);

			//先将form值放到view中
			TransferAcceptedInfo accept = new TransferAcceptedInfo();
			accept = accept.getAcceptInfoBySerial(view.getSerial(),"");
			
			view.setBasicCarId(accept.getBasicCarId());//库存关联ID
			if(form.getNewCarOwnerOld()!=null && !"".equals(form.getNewCarOwnerOld()))
			{
				accept.setNewcarOwner(form.getNewCarOwnerOld());
			}
			if(form.getNewcarOwnerIdCardNoOld()!=null && !"".equals(form.getNewcarOwnerIdCardNoOld()))
			{
				accept.setNewcarOwnerIdCardNo(form.getNewcarOwnerIdCardNoOld());
			}
			if(form.getNewOwnerOrgNoOld()!=null && !"".equals(form.getNewOwnerOrgNoOld()))
			{
				accept.setNewOwnerOrgNo(form.getNewOwnerOrgNoOld());
			}
			if(form.getNewOwnerAddressOld()!=null && !"".equals(form.getNewOwnerAddressOld()))
			{
				accept.setNewOwnerAddress(form.getNewOwnerAddressOld());
			}
			if(form.getNewOwnerPhoneOld()!=null && !"".equals(form.getNewOwnerPhoneOld()))
			{
				accept.setNewOwnerPhone(form.getNewOwnerPhoneOld());
			}
			if(form.getCarMarketInOld()!=null && !"".equals(form.getCarMarketInOld()))
			{
				accept.setCarMarketIn(form.getCarMarketInOld());
			}
			if(form.getTransferPriceOld()!=null && !"".equals(form.getTransferPriceOld()))
			{
				accept.setPrice(Integer.parseInt(StringUtil.removeDH(form.getTransferPriceOld())));
			}	
			if(form.getTaxNoOld()!=null && !"".equals(form.getTaxNoOld()))
			{
				accept.setTaxNo(form.getTaxNoOld());
			}
			if(form.getTransNoOld()!=null && !"".equals(form.getTransNoOld()))
			{
				accept.setTransNo(form.getTransNoOld());
			}
			if(form.getTransMarketOld()!=null && !"".equals(form.getTransMarketOld()))
			{
				accept.setTransMarket(form.getTransMarketOld());
			}
			if(form.getStatus()!=null && !"".equals(form.getStatus()))
			{
				accept.setStatus(Integer.parseInt(view.getStatus()));
			}
			if(form.getCarOwnerIdCardNoOld()!=null && !"".equals(form.getCarOwnerIdCardNoOld()))
			{
				accept.setCarOwnerIdCardNo(form.getCarOwnerIdCardNoOld());
			}
			if(form.getOwnerOrgNoOld()!=null && !"".equals(form.getOwnerOrgNoOld()))
			{
				accept.setOwnerOrgNo(form.getOwnerOrgNoOld());	
			}
			if(form.getOwnerPhoneOld()!=null && !"".equals(form.getOwnerPhoneOld()))
			{
				accept.setOwnerPhone(form.getOwnerPhoneOld());
			}
			if(form.getOwnerAddressOld()!=null && !"".equals(form.getOwnerAddressOld()))
			{
				accept.setOwnerAddress(form.getOwnerAddressOld());
			}
			if(form.getCarOwnerOld()!=null && !"".equals(form.getCarOwnerOld()))
			{
				accept.setCarOwner(form.getCarOwnerOld());
			}
			if(form.getSerial()!=null && !"".equals(form.getSerial()))
			{
				accept.setSerial(view.getSerial());
			}
			accept.setChangeTradeTicket(1);
			
			if(form.getNewAgentNameOld()!=null && !"".equals(form.getNewAgentNameOld()))
			{
				accept.setNewAgentName(form.getNewAgentNameOld());
			}
			if(form.getNewAgentOwnerIdCardNoOld()!=null && !"".equals(form.getNewAgentOwnerIdCardNoOld()))
			{
				accept.setNewAgentOwnerIdCardNo(form.getNewAgentOwnerIdCardNoOld());
			}
			if(form.getNewAgentAddressOld()!=null && !"".equals(form.getNewAgentAddressOld()))
			{
				accept.setNewAgentAddress(form.getNewAgentAddressOld());
			}
			if(form.getNewAgentPhoneOld()!=null && !"".equals(form.getNewAgentPhoneOld()))
			{
				accept.setNewAgentPhone(form.getNewAgentPhoneOld());
			}
			if(form.getOwnerTypeOld()!=null && !"".equals(form.getOwnerTypeOld())){
				accept.setOwnerType(Integer.parseInt(form.getOwnerTypeOld()));
			}
			if(form.getNewOwnerTypeOld()!=null && !"".equals(form.getNewOwnerTypeOld())){
				accept.setNewOwnerType(Integer.parseInt(form.getNewOwnerTypeOld()));
			}
			if(!"市".equals(form.getMoveOutCityOld())){
				accept.setMoveOutCity(form.getMoveOutCityOld());
			}
			if(!"省".equals(form.getMoveOutProvinceOld())){
				accept.setMoveOutProvince(form.getMoveOutProvinceOld());
			}
			if(form.getTransferTypeOld()!=null && !"".equals(form.getTransferTypeOld())){
				accept.setTransferType(Integer.parseInt(form.getTransferTypeOld()));
			}
			accept.updateAcceptInfoAtJYP(accept);

			if(accept.getMarketType() == 1)
			{
				Car car = new Car();
				car = car.findCarBySerial(view.getBasicCarId());
				car = setEntityFromViewhelper(car,view);
				car.setUpdateUserId(profile.getUniqueId());
				car.setStatus(3);
				if(accept.getSourceType()==2)
				{
					car.setStatus(4);
					car.setRegisterStatus(4);
				}
				// 更新库存表
				car.updateMarketInfoByLicensePlate(car);				
			}
			if(accept.getMarketType() == 2)
			{
				TransferPersonalCarInfo person = new TransferPersonalCarInfo();
				person = person.getPersonalCarInfoBySerial(view.getBasicCarId());
				person = setEntityFromViewhelper1(person, view);
				person.setUpdateUserId(profile.getUniqueId());			
				person.setStatus(3);
				if(accept.getSourceType()==2)
				{
					person.setStatus(4);
				}
				// 更新库存表
				person.updateCarBaseInfo(person);			
			}
			Codes code = Codes.getInstance();
			//历史表
			String reason = "";
			if(!"省".equals(view.getMoveOutProvinceOld())){
				reason += "【转出车管所省】从【"+view.getMoveOutProvince()+"】修改为【"+view.getMoveOutProvinceOld()+"】，";
			}
			if(!"市".equals(view.getMoveOutCityOld())){
				reason += "【转出车管所市】从【"+view.getMoveOutCity()+"】修改为【"+view.getMoveOutCityOld()+"】，";
			}
			if(view.getTransferTypeOld()!=null&&!"".equals(view.getTransferTypeOld())){
				String transfer ="";
				if("3".equals(view.getTransferType())){
					transfer ="过户";
				}else{
					transfer ="转出";
				}
				if("3".equals(view.getTransferTypeOld())){
					reason += "【受理事项】从【"+transfer+"】修改为【过户】，";
				}else{
					reason += "【受理事项】从【"+transfer+"】修改为【转出】，";
				}
				
			}
			if(view.getOwnerTypeOld()!=null&&!"".equals(view.getOwnerTypeOld())){
				String ownType ="";
				if("1".equals(view.getOwnerType())){
					ownType="个人";
				}else{
					ownType="企业";
				}
				if("1".equals(view.getOwnerTypeOld())){
					reason += "【车主类型】从【"+ownType+"】修改为【个人】，";
				}else{
					reason += "【车主类型】从【"+ownType+"】修改为【企业】，";
				}			
				
			}
			if(view.getNewOwnerTypeOld()!=null&&!"".equals(view.getNewOwnerTypeOld())){
				String ownTypeOld ="";
				if("1".equals(view.getNewOwnerType())){
					ownTypeOld="个人";
				}else{
					ownTypeOld="企业";
				}
				if("1".equals(view.getNewOwnerTypeOld())){
					reason += "【车主类型】从【"+ownTypeOld+"】修改为【个人】，";
				}else{
					reason += "【车主类型】从【"+ownTypeOld+"】修改为【企业】，";
				}			
				
			}
			
			if(view.getVendorCodeOld()!=null&&!"".equals(view.getVendorCodeOld()))
			{
				reason += "【厂牌】从【"+view.getVendorCode()+"】修改为【"+view.getVendorCodeOld()+"】，";
			}
			if(view.getModelCodeOld()!=null&&!"".equals(view.getModelCodeOld()))
				{
					reason += "【型号】从【"+view.getModelCode()+"】修改为【"+view.getModelCodeOld()+"】，";
				}			
			if(view.getRegisterCardNoOld()!=null && !"".equals(view.getRegisterCardNoOld()))
			{
				reason += "【登记证书编号】从【"+view.getRegisterCardNo()+"】修改为【"+view.getRegisterCardNoOld()+"】，";
			}
			if(view.getCarTypeOld()!=null && !"".equals(view.getCarTypeOld()))
			{
				reason += "【车辆类型】从【"+view.getCarType()+"】修改为【"+code.getItemValueByItemIdAndItemCode(Codes.VEHICLE_TYPE, Integer.parseInt(view.getCarTypeOld()))+"】，";
			}
			if(view.getCarMarketInOld()!=null&&!"".equals(view.getCarMarketInOld()))
			{
				reason += "【转入车管所】从【"+view.getCarMarketIn()+"】修改为【"+view.getCarMarketInOld()+"】，";
			}
			if(view.getCarOwnerOld()!=null&&!"".equals(view.getCarOwnerOld()))
			{
				reason += "【车主姓名】从【"+view.getCarOwner()+"】修改为【"+view.getCarOwnerOld()+"】，";
			}
			if(view.getCarOwnerIdCardNoOld()!=null&&!"".equals(view.getCarOwnerIdCardNoOld()))
			{
				reason += "【车主身份证号】从【"+view.getCarOwnerIdCardNo()+"】修改为【"+view.getCarOwnerIdCardNoOld()+"】，";
			}
			if(view.getOwnerOrgNoOld()!=null&&!"".equals(view.getOwnerOrgNoOld()))
			{
				reason += "【组织机构代码证号】从【"+view.getOwnerOrgNo()+"】修改为【"+view.getOwnerOrgNoOld()+"】，";
			}
			if(view.getOwnerAddressOld()!=null&&!"".equals(view.getOwnerAddressOld()))
			{
				reason += "【车主住址】从【"+view.getOwnerAddress()+"】修改为【"+view.getOwnerAddressOld()+"】，";
			}
			if(view.getOwnerPhoneOld()!=null&&!"".equals(view.getOwnerPhoneOld()))
			{
				reason += "【旧车主电话】从【"+view.getOwnerPhone()+"】修改为【"+view.getOwnerPhoneOld()+"】，";
			}
			if(view.getNewCarOwnerOld()!=null&&!"".equals(view.getNewCarOwnerOld()))
			{
				reason += "【新车主姓名】从【"+view.getNewCarOwner()+"】修改为【"+view.getNewCarOwnerOld()+"】，";
			}
			if(view.getNewcarOwnerIdCardNoOld()!=null&&!"".equals(view.getNewcarOwnerIdCardNoOld()))
			{
				reason += "【新车主身份证号】从【"+view.getNewcarOwnerIdCardNo()+"】修改为【"+view.getNewcarOwnerIdCardNoOld()+"】，";
			}
			if(view.getNewOwnerOrgNoOld()!=null&&!"".equals(view.getNewOwnerOrgNoOld()))
			{
				reason += "【新组织机构代码证号】从【"+view.getNewOwnerOrgNo()+"】修改为【"+view.getNewOwnerOrgNoOld()+"】，";
			}
			if(view.getNewOwnerAddressOld()!=null&&!"".equals(view.getNewOwnerAddressOld()))
			{
				reason += "【新车主住址】从【"+view.getNewOwnerAddress()+"】修改为【"+view.getNewOwnerAddressOld()+"】，";
			}
			if(view.getNewOwnerPhoneOld()!=null&&!"".equals(view.getNewOwnerPhoneOld()))
			{
				reason += "【移动电话】从【"+view.getNewOwnerPhone()+"】修改为【"+view.getNewOwnerPhoneOld()+"】，";
			}
			if(view.getTransferPriceOld()!=null&&!"".equals(view.getTransferPriceOld()))
			{
				reason += "【交易价格(元)】从【"+view.getTransferPrice()+"】修改为【"+view.getTransferPriceOld()+"】，";
			}
			if(view.getTaxNoOld()!=null&&!"".equals(view.getTaxNoOld()))
			{
				reason += "【增值税完税证号】从【"+view.getTaxNo()+"】修改为【"+view.getTaxNoOld()+"】，";
			}
			if(view.getTransNoOld()!=null&&!"".equals(view.getTransNoOld()))
			{
				reason += "【 交易票发票号码 】从【"+view.getTransNo()+"】修改为【"+view.getTransNoOld()+"】，";
			}
			if(view.getTransMarketOld()!=null&&!"".equals(view.getTransMarketOld()))
			{
				TransMarketCode marketCode = TransMarketCode.getInstance();
				marketCode = marketCode.getTransMarketCodeInfo(view.getTransMarketOld());
				reason += "【二手车市场】从【"+view.getTransMarket()+"】修改为【"+marketCode.getMarketName()+"】，";
			}
			if(view.getNewAgentNameOld()!=null&&!"".equals(view.getNewAgentNameOld()))
			{
				reason += "【 新车主代理人姓名】从【"+accept.getNewAgentName()+"】修改为【"+view.getNewAgentNameOld()+"】，";
			}
			if(view.getNewAgentOwnerIdCardNoOld()!=null&&!"".equals(view.getNewAgentOwnerIdCardNoOld()))
			{
				reason += "【 新车主代理人身份证号】从【"+accept.getNewAgentOwnerIdCardNo()+"】修改为【"+view.getNewAgentOwnerIdCardNoOld()+"】，";
			}
			if(view.getNewAgentAddressOld()!=null&&!"".equals(view.getNewAgentAddressOld()))
			{
				reason += "【 新车主代理人住址】从【"+accept.getNewAgentAddress()+"】修改为【"+view.getNewAgentAddressOld()+"】，";
			}
			if(view.getNewAgentPhoneOld()!=null&&!"".equals(view.getNewAgentPhoneOld()))
			{
				reason += "【 新车主代理人电话】从【"+accept.getNewAgentPhone()+"】修改为【"+view.getNewAgentPhoneOld()+"】，";
			}
			
			if(view.getReason()!=null && !"".equals(view.getReason()))
			{
				reason +="【备注】："+view.getReason()+"";
			}
			if(reason!=null && !"".equals(reason))
			{
				if(reason.substring(reason.length()-1,reason.length()).equals("，"))
				{
					reason = reason.substring(0,reason.length()-1);
				}
			}

			TransferProcessHistory history = new TransferProcessHistory();
			history.setSerial(view.getSerial());
			history.setBusinessType(23);
			history.setInsertUserId(profile.getUniqueId());
			history.setReason(reason);
			history.insertHisInfoAtAssess(history);
			uc.setAttribute("serial",view.getSerial());
			uc.setAttribute(TmsConstants.VIEWHELP, view);
			controller.commit();
		} catch (Exception e) {
			try {
				controller.rollback();
			} catch (TransactionException e1) {
				throw new TmsException(TmsException.DB_TXCRROR, e1);
			}
			// 系统异常
			if (e instanceof TmsException) {
				throw e;
			}
			throw new TmsException(TmsException.DB_TXCRROR, e);
		}
		logger.debug(this.getClass().getName() + "#execute was completed.");
	}
	/**
	 * 
	 * 将viewhelp中值set到entity中
	 * 
	 * @throws TmsDaoException
	 */
	private Car setEntityFromViewhelper(Car entity, TransferRgInputViewHelper view)
			throws TmsDaoException {
	
		entity.setSerial(view.getBasicCarId());
		if(view.getVendorCodeOld()!=null && !"".equals(view.getVendorCodeOld()))
		{
			entity.setVendorCode(view.getVendorCodeOld());
		}
		if(view.getModelCodeOld()!=null && !"".equals(view.getModelCodeOld()))
		{
			entity.setModelCode(view.getModelCodeOld());
		}
		if(view.getCarTypeOld()!=null && !"".equals(view.getCarTypeOld()))
		{
			entity.setCarType(Integer.parseInt(view.getCarTypeOld()));
		}
		if(view.getRegisterCardNoOld()!=null && !"".equals(view.getRegisterCardNoOld()))
		{
			entity.setRegisterCardNo(view.getRegisterCardNoOld());
		}
		if(view.getCarOwnerOld()!=null && !"".equals(view.getCarOwnerOld()))
		{
			entity.setCarOwner(view.getCarOwnerOld());
		}
		if(view.getCarOwnerIdCardNoOld()!=null && !"".equals(view.getCarOwnerIdCardNoOld()))
		{
			entity.setCarOwnerIdCardNo(view.getCarOwnerIdCardNoOld());		
		}
		if(view.getOwnerOrgNoOld()!=null && !"".equals(view.getOwnerOrgNoOld()))
		{
			entity.setOwnerOrgNo(view.getOwnerOrgNoOld());
		}
		if(view.getOwnerAddressOld()!=null && !"".equals(view.getOwnerAddressOld()))
		{
			entity.setOwnerAddress(view.getOwnerAddressOld());
		}
		if(view.getOwnerPhoneOld()!=null && !"".equals(view.getOwnerPhoneOld()))
		{
			entity.setOwnerPhone(view.getOwnerPhoneOld());
		}
		if(view.getOwnerTypeOld() != null && !"".equals(view.getOwnerTypeOld())){
			entity.setOwnerType(Integer.parseInt(view.getOwnerTypeOld()));
		}
		
		
		return entity;
	}
	/**
	 * 
	 * 将viewhelp中值set到entity中
	 * 
	 * @throws TmsDaoException
	 */
	private TransferPersonalCarInfo setEntityFromViewhelper1(TransferPersonalCarInfo entity, TransferRgInputViewHelper view)
			throws TmsDaoException {
		entity.setSerial(view.getBasicCarId());
		if(view.getVendorCodeOld()!=null && !"".equals(view.getVendorCodeOld()))
		{
			entity.setVendorCode(view.getVendorCodeOld());
		}
		if(view.getModelCodeOld()!=null && !"".equals(view.getModelCodeOld()))
		{
			entity.setModelCode(view.getModelCodeOld());
		}
		if(view.getCarTypeOld()!=null && !"".equals(view.getCarTypeOld()))
		{
			entity.setCarType(Integer.parseInt(view.getCarTypeOld()));
		}
		if(view.getRegisterCardNoOld()!=null && !"".equals(view.getRegisterCardNoOld()))
		{
			entity.setRegisterCardNo(view.getRegisterCardNoOld());
		}
		if(view.getCarOwnerOld()!=null && !"".equals(view.getCarOwnerOld()))
		{
			entity.setCarOwner(view.getCarOwnerOld());
		}
		if(view.getCarOwnerIdCardNoOld()!=null && !"".equals(view.getCarOwnerIdCardNoOld()))
		{
			entity.setCarOwnerIdCardNo(view.getCarOwnerIdCardNoOld());		
		}
		if(view.getOwnerOrgNoOld()!=null && !"".equals(view.getOwnerOrgNoOld()))
		{
			entity.setOwnerOrgNo(view.getOwnerOrgNoOld());
		}
		if(view.getOwnerAddressOld()!=null && !"".equals(view.getOwnerAddressOld()))
		{
			entity.setOwnerAddress(view.getOwnerAddressOld());
		}
		if(view.getOwnerPhoneOld()!=null && !"".equals(view.getOwnerPhoneOld()))
		{
			entity.setOwnerPhone(view.getOwnerPhoneOld());
		}
		if(view.getOwnerTypeOld() != null && !"".equals(view.getOwnerTypeOld())){
			entity.setOwnerType(Integer.parseInt(view.getOwnerTypeOld()));
		}
		return entity;
	}
}
